package com.lzb.randomturntable.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.SizeUtils;
import com.lzb.randomturntable.R;

public class TurnTableContainer extends FrameLayout {

    private TurnTableView turnTableView;

    public TurnTableContainer(@NonNull Context context) {
        this(context, null);
    }

    public TurnTableContainer(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TurnTableContainer(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TurnTableContainer);
        Drawable pointerDrawable = array.getDrawable(R.styleable.TurnTableContainer_ttc_pointerDrawable);
        Drawable bgDrawable = array.getDrawable(R.styleable.TurnTableContainer_ttc_bgDrawable);
        int padding = array.getDimensionPixelSize(R.styleable.TurnTableContainer_ttc_padding, 0);
        int textSize = array.getDimensionPixelSize(R.styleable.TurnTableContainer_ttc_textSize, SizeUtils.sp2px(15));
        array.recycle();

        //add view
        TurnTableView turnTableView = new TurnTableView(getContext());
        this.turnTableView = turnTableView;
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        layoutParams.gravity = Gravity.CENTER;
        turnTableView.setTextSize(textSize);
        turnTableView.setPaddingRelative(padding, padding, padding, padding);
        turnTableView.setBackground(bgDrawable);
        addView(turnTableView, layoutParams);

        ImageView imageView = new ImageView(getContext());
        layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER;
        imageView.setImageDrawable(pointerDrawable);
        addView(imageView, layoutParams);
    }

    public void setAdapter(TurnTableView.Adapter adapter) {
        turnTableView.setAdapter(adapter);
    }

    public TurnTableView.Adapter getAdapter() {
        return turnTableView.getAdapter();
    }

    public void startRotate() {
        turnTableView.startRotate();
    }

    public void setListener(TurnTableView.IListener listener) {
        turnTableView.setListener(listener);
    }

}
